package com.yyh.fanshe;

import java.lang.reflect.Field;

public class Demo10 {
    private Demo10(){

    }

    public static void main(String[] args) {
        User[] users = new User[]{
                new User("张三",18,"男","上海"),
                new User("张三",18,"男","上海"),
                new User("张三",18,"男","上海"),
                new User("张三",18,"男","上海"),
                new User("张三",18,"男","上海"),
        };
        String json = oneListToJson(users);
        System.out.println("json = " + json);
    }
    //定义一个方法：将单个对象的json格式的字符串转为java对象
    public static<T> String oneJSONToObject(String json, Class<T> clazz) throws InstantiationException, IllegalAccessException, NoSuchFieldException {
        //检验传入的参数
        if(json == null || json.length() == 0){
            return null;
        }
        //去除最外层的大括号
        json = json.substring(1,json.length()-1);
        //创建目标类的实例
        T t = clazz.newInstance();
        //用逗号分隔属性键值对
        String[] keyValues = json.split(",");
        for (String keyValue : keyValues) {
            //用等号分隔键和值
            String[] entry = keyValue.split("=");
            String key = entry[0];
            String value = entry[1];
            //获取属性值
            Field field = clazz.getDeclaredField(key);
            field.setAccessible(true);
            field.set(t,value);
            System.out.println("field = " + field);
        }


        return null;
    }
    //定义一个方法：将java中的集合对象转为json字符串
    public static<T> String oneListToJson(T[] t) {
        //检验传入的参数
        if(t == null){
            return null;
        }
        //不为空时，创建一个StringBuilder对象
        StringBuilder sb = new StringBuilder();
        sb.append("[");
        for (T t1 : t) {
            String json = oneObjectToJson(t1);
            sb.append(json + ",");
        }
        sb.deleteCharAt(sb.length()-1);
        sb.append("]");
        return sb.toString();

    }
    //将简单对象User user转为json字符串
    public static<T> String oneObjectToJson(T t) {
        try {
            //检验传入的参数
            if(t == null){
                return null;
            }
            //当参数不为空时，将user对象转为字节码对象
            Class clas1 = t.getClass();
            //获取User类对象中的所有的Field对象
            Field[] fields = clas1.getDeclaredFields();
            //创建一个StringBuilder对象
            StringBuilder sb = new StringBuilder();
            sb.append("{");
            //遍历Field对象
            for (Field field : fields) {
                String fieldName = field.getName();
                if (fieldName.equals("serialVersionUID")){
                    continue;
                }
                //获取Field对象中的属性值
                field.setAccessible(true);
                Object fieldvalue = field.get(t);
                //获取Field对象中的属性值的类型
                Class<?> fieldType = field.getType();
                //判断属性值的类型是否是字符串有关的类型
                if(fieldType == String.class || fieldType == StringBuffer.class || fieldType == StringBuilder.class ){
                    fieldvalue = "\"" + fieldvalue + "\"";
                }
                sb.append("\"" + fieldName + "\"" + ":" + fieldvalue + ",");
            }
            //删除最后一个逗号
            sb.deleteCharAt(sb.length()-1);
            sb.append("}");
            return sb.toString();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }

        return null;
    }
}
